package key.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * User: key
 * ClassName:PaymentUtils
 * Date: 2023/5/8
 * Time: 22:35
 * description:
 */
public class PaymentUtils {
    private static String encodingCharset = "UTF-8";
    public static String buildHmac(String p0_Cmd,String p1_MerId,String p2_Order,String p3_Amt,
                                   String p4_Cur,String p5_Pid,String p6_Pcat,String p7_Pdesc,
                                   String p8_Url,String p9_Saf,String pa_Mp,String pd_FrpId,
                                   String pr_NeedResponse,String keyValue){
        StringBuilder sValue = new StringBuilder();
        sValue.append(p0_Cmd);
        sValue.append(p1_MerId);
        sValue.append(p2_Order);
        sValue.append(p3_Amt);
        sValue.append(p4_Cur);
        sValue.append(p5_Pid);
        sValue.append(p6_Pcat);
        sValue.append(p7_Pdesc);
        sValue.append(p8_Url);
        sValue.append(p9_Saf);
        sValue.append(pa_Mp);
        sValue.append(pd_FrpId);
        sValue.append(pr_NeedResponse);
        return PaymentUtils.hmacSign(sValue.toString(),keyValue);
    }
    public static boolean verifyCallback(String hmac, String p1_MerId,
                                         String r0_Cmd, String r1_Code, String r2_TrxId, String r3_Amt,
                                         String r4_Cur, String r5_Pid, String r6_Order, String r7_Uid,
                                         String r8_MP, String r9_BType, String keyValue) {
        StringBuilder sValue = new StringBuilder();
        sValue.append(p1_MerId);
        sValue.append(r0_Cmd);
        sValue.append(r1_Code);
        sValue.append(r2_TrxId);
        sValue.append(r3_Amt);
        sValue.append(r4_Cur);
        sValue.append(r5_Pid);
        sValue.append(r6_Order);
        sValue.append(r7_Uid);
        sValue.append(r8_MP);
        sValue.append(r9_BType);
        String newString = PaymentUtils.hmacSign(sValue.toString(), keyValue);
        return newString.equals(hmac);
    }

    private static String hmacSign(String aValue, String aKey) {
        byte k_ipad[] = new byte[64];
        byte k_opad[] = new byte[64];
        byte keyb[];
        byte value[];
        try {
            keyb = aKey.getBytes(encodingCharset);
            value = aValue.getBytes(encodingCharset);
        } catch (UnsupportedEncodingException e) {
            keyb = aKey.getBytes();
            value = aValue.getBytes();
        }
        Arrays.fill(k_ipad,keyb.length,64,(byte) 54);
        Arrays.fill(k_opad,keyb.length,64,(byte) 92);
        for (int i = 0; i < keyb.length; i++) {
            k_ipad[i] = (byte) (keyb[i] ^ 0x36);
            k_opad[i] = (byte) (keyb[i] ^ 0x5c);
        }
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        md.update(k_ipad);
        md.update(value);
        byte dg[] = md.digest();
        md.reset();
        md.update(k_opad);
        md.update(dg,0,16);
        dg = md.digest();
        return toHex(dg);
    }

    private static String toHex(byte[] dg) {
        if(dg == null)return null;
        StringBuffer output = new StringBuffer(dg.length * 2);
        for (int i = 0; i < dg.length; i++) {
            int current = dg[i] & 0xff;
            if (current<16)output.append(0);
            output.append(Integer.toString(current,16));
        }
        return output.toString();
    }
    public static String getHmac(String[] args,String key){
        if(args == null || args.length == 0){
            return (null);
        }
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < args.length; i++) {
            str.append(args[i]);
        }
        return (hmacSign(str.toString(),key));
    }
    public static String digest(String aValue){
        aValue = aValue.trim();
        byte value[];
        try {
            value = aValue.getBytes(encodingCharset);
        } catch (UnsupportedEncodingException e) {
            value = aValue.getBytes();
        }
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
        return toHex(md.digest(value));
    }
}
